#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("标准对话框示例");

    m_fileBtn=new QPushButton("文件标准对话框");
    m_fileLineEdit=new QLineEdit;

    m_colorBtn=new QPushButton("颜色标准对话框");
    m_colorFrame=new QFrame;     //当部件没有指定父类，自己为顶级部件，当添加到布局管理器时，布局管理器有父类，该部件会重新定义自己的父类
    //设置边框风格
    m_colorFrame->setFrameStyle(QFrame::Box);
    //设置填充背景属性
    m_colorFrame->setAutoFillBackground(true);

    m_fontBtn=new QPushButton("字体标准对话框");
    m_fontLineEdit=new QLineEdit("大卉—MrTring");

    m_inputBtn=new QPushButton("标准输入对话框");
    m_msgBtn=new QPushButton("标准消息对话框");

    m_customBtn=new QPushButton("自定义消息框");
    m_customLabel=new QLabel;
    m_customLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);



    m_mainLayout=new QGridLayout(this);
    m_mainLayout->addWidget(m_fileBtn,0,0);
    m_mainLayout->addWidget(m_fileLineEdit,0,1);
    m_mainLayout->addWidget(m_colorBtn,1,0);
    m_mainLayout->addWidget(m_colorFrame,1,1);
    m_mainLayout->addWidget(m_fontBtn,2,0);
    m_mainLayout->addWidget(m_fontLineEdit,2,1);
    m_mainLayout->addWidget(m_inputBtn,3,0);
    m_mainLayout->addWidget(m_msgBtn,3,1);
    m_mainLayout->addWidget(m_customBtn,4,0);
    m_mainLayout->addWidget(m_customLabel,4,1);

    connect(m_fileBtn,&QPushButton::clicked,this,&Dialog::ShowFileDlg);
    connect(m_colorBtn,&QPushButton::clicked,this,&Dialog::ShowColorDlg);
    connect(m_fontBtn,&QPushButton::clicked,this,&Dialog::ShowFontDlg);
    connect(m_inputBtn,&QPushButton::clicked,this,&Dialog::ShowInputDlg);
    connect(m_msgBtn,&QPushButton::clicked,this,&Dialog::ShowMsgDlg);

    connect(m_customBtn,&QPushButton::clicked,this,&Dialog::showCustomDlg);

}

Dialog::~Dialog()
{

}

//当点击按钮时会选择打开一个文件，并将该文件路径显示到m_fileLineEdit中
void Dialog::ShowFileDlg()
{
   //QString sPath= QFileDialog::getOpenFileName(this,"标准对话框",".","C++ files(*.cpp);;C file(*.c);;Header file(*.h)");
   QString sPath= QFileDialog::getOpenFileName(this,"标准对话框",
                                 ".",
                                 "C++ files(*.cpp);;"
                                 "C file(*.c);;"
                                 "Header file(*.h)");

   m_fileLineEdit->setText(sPath);
}

void Dialog::ShowColorDlg()
{
    QColor color=QColorDialog::getColor(Qt::yellow);
    if(color.isValid())
        m_colorFrame->setPalette(QPalette(color));

}

void Dialog::ShowFontDlg()
{
    bool ok;
   QFont font= QFontDialog::getFont(&ok);    // 返回的是字体
   if(ok)
       m_fontLineEdit->setFont(font);
}

void Dialog::ShowInputDlg()
{
    m_inputDlg=new InputDlg(this);
    m_inputDlg->show();
}

void Dialog::ShowMsgDlg()
{
    m_msgboxDlg=new MsgBoxDlg(this);
    m_msgboxDlg->show();
}

void Dialog::showCustomDlg()
{
    m_customLabel->setText("自定义消息框");
    QMessageBox customMsgBox;
    customMsgBox.setWindowTitle("自定义消息框");

    QPushButton *yes=customMsgBox.addButton("QK",QMessageBox::ActionRole);
    QPushButton* no=customMsgBox.addButton("NO",QMessageBox::ActionRole);
    QPushButton* cancel=customMsgBox.addButton(QMessageBox::Cancel);

    customMsgBox.setIconPixmap(QPixmap("msg.png"));
    customMsgBox.exec();

    if(customMsgBox.clickedButton()==yes)
        m_customLabel->setText("点了，好吧!");
    if(customMsgBox.clickedButton()==no)
        m_customLabel->setText("点了，算了吧!");
    if(customMsgBox.clickedButton()==cancel)
        m_customLabel->setText("点了，取消");
}






















